package com.gopay.scheduler.job;

import com.gopay.common.cps.dao.acct.CommonQueryDAO;
import com.gopay.common.util.DateUtils;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.sms.client.GopaySMSHttpClient;
import com.gopay.sms.common.constants.SMSAppId;
import com.gopay.sms.common.domain.RequestVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by zhaojt on 2017/4/28.
 * 懒猫手续费支出账户余额不足提醒任务
 */
public class DFSAFeeAcctAvaAmtAlarmJob extends GopayBaseJob{

    private Logger logger = LoggerFactory.getLogger(getClass());

    private boolean on ;

    private String mobile;

    @Resource(name = "comQueryDao")
    private CommonQueryDAO commonQueryDAO;

    @Override
    public void doJobInternal() {
        logger.info("开始执行懒猫手续费账户余额不足提醒任务...");

        if(on){
            List<Object[]> result = commonQueryDAO.findBySql(buildSql());

            for(Object[] record : result){
                String custId = (String)record[0];
                String corpName = (String)record[1];
                String acctId = (String)record[2];
                BigDecimal avaAmt = (BigDecimal)record[3];
                BigDecimal balAmt = (BigDecimal)record[4];

                logger.info("record : {}", record);

                //edit by zhaojt 目前只有懒猫接了资金存管，portal没有配置功能
                //此处判断写死
                if("0000000002000008367".equals(acctId)){
                    logger.info("执行通知。。。");
                    //3月23日13：05
                    String content = "截止%s存管手续费账户 %s 余额已低于您设定的最低额度8万，为不影响用户的正常交易，请尽快登陆国付宝后台进行充值";
                    sendSmsNotify(String.format(content, DateUtils.format(new Date(), "MM月dd日HH:mm"), acctId));
                    logger.info("通知结束。");
                }else {
                    logger.info("非同城翼龙贷不通知。");
                }

            }

        }else{
            logger.info("开关关闭。再见!");
        }

        logger.info("任务结束...");
    }

    private String buildSql(){
        StringBuilder sb = new StringBuilder();
        sb.append(" select * from ");
        sb.append(" (select ");
        sb.append(" t1.cust_id, c.corp_name, t1.acct_id, t1.ava_amt, t1.bal_amt, row_number() over(partition by t1.cust_id order by t1.acct_id ) rn");
        sb.append(" from acps_acct_mast t1 ");
        sb.append(" left join cust_corp_info c ");
        sb.append(" on t1.cust_id = c.cust_id");
        sb.append(" where exists ( select t2.cust_id from acps_acct_mast t2 where t2.prod_code = 'DFSA' and t1.cust_id = t2.cust_id)");
        sb.append(" and t1.prod_code = 'GNST'");
        sb.append(" ) tmp where tmp.rn = 1 and tmp.ava_amt < 80000 ");
        return sb.toString();
    }


    /**
     * 发送短信
     */
    private void sendSmsNotify(String content){
        try {
            RequestVO req = new RequestVO();
            req.setAppId(SMSAppId._11005.value);
            req.setMobile(mobile);//如果发送多个手机号码时，可使用逗号进行隔开。
            req.setContent(content);
            logger.info("通知入参： req :{}" , req);
            GopaySMSHttpClient.getInstance().createVerificationCode(req);
        } catch (Exception e) {
            logger.error("发送短信通知时异常", e);
        }
    }

    public boolean isOn() {
        return on;
    }

    public void setOn(boolean on) {
        this.on = on;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public CommonQueryDAO getCommonQueryDAO() {
        return commonQueryDAO;
    }

    public void setCommonQueryDAO(CommonQueryDAO commonQueryDAO) {
        this.commonQueryDAO = commonQueryDAO;
    }
}
